/**
 * 2019年3月13日 上午9:26:55  
 * richest-api  
 * @author zhaohongchao
 * @version 1.0
 * EatOrderServiceImpl.java  
 */
package com.richest.api.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.richest.api.dao.DiningTableMapper;
import com.richest.api.dao.EatOrderMapper;
import com.richest.api.dao.FoodOrderMapper;
import com.richest.api.service.IEatOrderService;
import com.richest.bean.DiningTableBean;
import com.richest.bean.EatOrderBean;
import com.richest.util.CallResults;

/**
 * 2019年3月13日 上午9:26:55 richest-api
 * 
 * @author zhaohongchao
 * @version 1.0 EatOrderServiceImpl.java
 */
@Service
public class EatOrderServiceImpl implements IEatOrderService {
	@Autowired
	private EatOrderMapper eatOrderMapper;
	@Autowired
	private FoodOrderMapper foodOrderMapper;
//	@Autowired
//	private UserMapper userMapper; 屏蔽没有意义资源消耗
	@Autowired
	private DiningTableMapper diningTableMapper;

	@Override
	public List<Map<String, Object>> selectEatOrderList(Integer status, String userId) {
		Map<String, Object> params = new HashMap<>();
		params.put("status", status);
		params.put("userId", userId);
		return eatOrderMapper.selectEatOrderList(params);
	}

	@Override
	public void insert(EatOrderBean eatOrderBean) {
		eatOrderMapper.insert(eatOrderBean);
	}

	@Override
	public EatOrderBean queryByEatSerialNumber(String eatSerialNumber, Integer status) {
		return eatOrderMapper.queryByEatSerialNumber(eatSerialNumber, status);
	}

	@Override
	public void update(EatOrderBean eatOrderBean) {
		eatOrderMapper.update(eatOrderBean);
	}

	@Override
	public CallResults getEatOrderBySerialNumber(String eatSerialNumber) {
		Map<String, Object> mapResult = new HashMap<>();
		// 查询订单信息
		Map<String, Object> mapOrder = eatOrderMapper.selectEatOrderListBySerialNumber(eatSerialNumber);
		if (null == mapOrder || mapOrder.size() <= 0) {
			return CallResults.error("不存在有效的订单");
		}
		mapResult.put("order", mapOrder);
		// 查询菜品信息
		List<Map<String, Object>> lstFoodOrder = foodOrderMapper
				.selectList(String.valueOf(mapOrder.get("eat_serial_number")));
		mapResult.put("list", lstFoodOrder);

		Map<String, Object> mapUserInfo = eatOrderMapper
				.getAppointment(String.valueOf(mapOrder.get("eat_serial_number")));
		mapResult.put("appointment", mapUserInfo);
		return CallResults.ok().put("data", mapResult);
	}

	/**
	 * 结算订单
	 * 
	 * @author zhaohongchao 2019-03-21 11:26:40
	 */
	@Override
	public String SettlementOrder(JSONObject params) {
		String payCallBackId = params.getString("pay_callback_id");
		String payCallBackTableName = params.getString("pay_callback_table_name");
		String eatSerialNumber = params.getString("eat_serial_number");
		String strOpenId = params.getString("open_id");
		String strPayMode = params.getString("pay_mode");
		BigDecimal bigDReceiptAmount = new BigDecimal(params.getString("receipt_amount"));

		EatOrderBean eatOrderBean = eatOrderMapper.queryByEatSerialNumber(eatSerialNumber, null);
		if (null == eatOrderBean) {
			return "1";
		}
		if (eatOrderBean.getPay_money().compareTo(bigDReceiptAmount) != 0) {
			return "2";
		}
		eatOrderBean.setStatus(5);
		eatOrderBean.setStatus_name("订单支付完成");
		eatOrderBean.setStatus_desc("感谢您的支持");
		eatOrderBean.setPay_callback_id(payCallBackId);
		eatOrderBean.setPay_callback_table_name(payCallBackTableName);
		eatOrderBean.setPay_all(1);
		eatOrderBean.setOpen_id(strOpenId);
		eatOrderBean.setPay_mode(strPayMode);

		// 更新桌/包房状态为已使用，用户走之后手动将状态设置成未使用
		DiningTableBean diningTableBean = new DiningTableBean();
		diningTableBean.setDining_table_id(eatOrderBean.getDining_table_id());
		diningTableBean.setStatus(2);
		diningTableBean.setStatus_name("已使用");
		diningTableBean.setBegin_time(eatOrderBean.getBegin_time());
		diningTableBean.setEnd_time(eatOrderBean.getEnd_time());

		diningTableMapper.updateStatus(diningTableBean);

		eatOrderMapper.update(eatOrderBean);

		return "0";
	}

	@Override
	public void appapplyRefundOrder(String eatSerialNumber, Integer orderStatus, String reason, String userId, Integer status,
			String status_name, String status_desc) {
		EatOrderBean eatOrderBean = eatOrderMapper.queryByEatSerialNumber(eatSerialNumber, orderStatus);
		eatOrderBean.setReason(reason);
		eatOrderBean.setStatus(7);
		eatOrderBean.setStatus_name("商户进行退款审核");
		eatOrderBean.setStatus_desc("等待商家审核，请联系商家确认");
		eatOrderBean.setUpdate_time(new Date());
		eatOrderBean.setUpdate_by(userId);

		// 更新桌/包房状态为未使用，用户走之后手动将状态设置成未使用
		DiningTableBean diningTableBean = new DiningTableBean();
		diningTableBean.setDining_table_id(eatOrderBean.getDining_table_id());
		diningTableBean.setStatus(1);
		diningTableBean.setStatus_name("未使用");
		diningTableBean.setBegin_time(null);
		diningTableBean.setEnd_time(null);

		diningTableMapper.updateStatus(diningTableBean);

		eatOrderMapper.update(eatOrderBean);
	}

	@Override
	public EatOrderBean selectEatOrderByUser(Long lUserId) {
		return eatOrderMapper.selectEatOrderByUser(lUserId);
	}

}
